# Maintainer: Taner Tas <taner76@gmail.com>
pkgname=mame
pkgver=0.251
_pkgver=${pkgver/.}
_groovymame=gm0251sr002o
pkgrel=0
pkgdesc="Multi Arcade Machine Emulator with GroovyMAME/Switchres/No-nag patchset."
url="https://mamedev.org/"
# riscv64: takes forever to build and constantly fails
# s390x: who plays an arcade emulator on a mainframe
arch="all !s390x !riscv64"
license="GPL-2.0-or-later"
depends="$pkgname-common"
makedepends="
	alsa-lib-dev
	coreutils
	expat-dev
	flac-dev
	fontconfig-dev
	glm-dev
	libjpeg-turbo-dev
	libxi-dev
	libxinerama-dev
	libxrandr-dev
	lua5.3-dev
	mesa-dev
	nasm
	portaudio-dev
	portmidi-dev<=200
	pulseaudio-dev
	python3-dev
	py3-sphinx
	rapidjson-dev
	sdl2-dev
	sdl2_ttf-dev
	sqlite-dev
	zlib-dev
	"
subpackages="
	$pkgname-common::noarch
	$pkgname-arcade
	$pkgname-mess
	$pkgname-plugins::noarch
	$pkgname-doc
	$pkgname-lang
	$pkgname-tools
	$pkgname-data::noarch
	"
source="
	https://github.com/mamedev/mame/archive/mame$_pkgver.tar.gz
	$_groovymame.patch::https://github.com/antonioginer/GroovyMAME/releases/download/$_groovymame/GroovyMAME${_groovymame:3:3}.patch
	mame.ini
	midi.conf
	fix-musl.patch
	nonag.patch
	"
options="!check" # No test suite
builddir="$srcdir"/$pkgname-$pkgname${pkgver/.}

build() {
	case "$CARCH" in
		x86|armhf|armv7) _PTR64=0;;
		*) _PTR64=1;;
	esac

	case "$CARCH" in
		x86_64|x86) _NOASM=0 ;;
		*) _NOASM=1 ;;
	esac

	export CFLAGS="$CFLAGS -I/usr/include/lua5.3 -DBX_CRT_MUSL"
	export CXXFLAGS="$CXXFLAGS -I/usr/include/lua5.3 -DBX_CRT_MUSL"
	export LDFLAGS="$LDFLAGS -L/usr/lib/lua5.3"

	local build="make
		TARGETOS=linux
		NOWERROR=1
		DEBUG=0
		USE_QTDEBUG=0
		VERBOSE=1
		REGENIE=1
		OSD=sdl
		SYMBOLS=0
		FORCE_DRC_C_BACKEND=
		PTR64=$_PTR64
		NOASM=$_NOASM
		BIGENDIAN=0
		TOOLS=
		PYTHON_EXECUTABLE=python3
		SDL_INI_PATH=/etc/mame
		USE_SYSTEM_LIB_EXPAT=1
		USE_SYSTEM_LIB_ZLIB=1
		USE_SYSTEM_LIB_JPEG=1
		USE_SYSTEM_LIB_FLAC=1
		USE_SYSTEM_LIB_LUA=1
		USE_SYSTEM_LIB_SQLITE3=1
		USE_SYSTEM_LIB_PORTAUDIO=1
		USE_SYSTEM_LIB_UV=1
		USE_SYSTEM_LIB_GLM=1
		USE_SYSTEM_LIB_RAPIDJSON=1
		USE_SYSTEM_LIB_PORTMIDI=1
		USE_SYSTEM_LIB_ASIO=0
		"

	[ -f mame ] || [ -f mame64 ] || $build TOOLS=1 SUBTARGET=mame
	[ -f mamearcade ] || [ -f mamearcade64 ] || $build SUBTARGET=arcade
	[ -f mess ] || [ -f mess64 ] || $build SUBTARGET=mess
}

package() {
	install -D -m755 mame "$pkgdir"/usr/bin/mame || \
	install -D -m755 mame64 "$pkgdir"/usr/bin/mame
	install -D -m644  docs/man/mame.6  "$pkgdir"/usr/share/man/man6/mame.6
	for i in castool.1 \
		chdman.1 \
		floptool.1 \
		imgtool.1 \
		jedutil.1 \
		ldresample.1 \
		ldverify.1 \
		romcmp.1
	do
		install -D -m644 docs/man/"$i"  "$pkgdir"/usr/share/man/man1/"$i"
	done

	install -D -m644 "$srcdir"/mame.ini "$pkgdir"/etc/mame/mame.ini
	install -D -m644 "$srcdir"/midi.conf "$pkgdir"/usr/lib/modules-load.d/midi.conf
	install -d -m755 "$pkgdir"/usr/share/mame/roms

	install -D -m755 mamearcade "$pkgdir"/usr/bin/mamearcade || \
		install -D -m755 mamearcade64 "$pkgdir"/usr/bin/mamearcade
	ln -s mamearcade "$pkgdir"/usr/bin/arcade

	install -D -m755 mamemess "$pkgdir"/usr/bin/mess || \
		install -D -m755 mamemess64 "$pkgdir"/usr/bin/mess

	for i in artwork \
		bgfx \
		hash \
		hlsl \
		ini \
		keymaps \
		language \
		plugins \
		samples
	do
		cp -r "$i" "$pkgdir"/usr/share/mame
	done
	rm -rf "$pkgdir"/usr/share/mame/bgfx/shaders/dx11
	rm -rf "$pkgdir"/usr/share/mame/bgfx/shaders/dx9
	rm -rf "$pkgdir"/usr/share/mame/bgfx/shaders/metal

	for i in castool \
		chdman \
		floptool \
		imgtool \
		jedutil \
		ldresample \
		ldverify \
		romcmp
	do
		install -Dm755 "$i" -t "$pkgdir"/usr/bin
	done
}

common() {
	pkgdesc="MAME - Common configuration files"

	amove etc/mame/mame.ini \
		usr/lib/modules-load.d/midi.conf \
		usr/share/mame/roms
}

arcade() {
	pkgdesc="Multi Arcade Machine Emulator - Arcade specific build."

	amove usr/bin/mamearcade \
		usr/bin/arcade
}

mess() {
	pkgdesc="Multi Arcade Machine Emulator - Mess specific build."

	amove usr/bin/mess
}

data() {
	pkgdesc="Distribution data files for MAME"

	amove usr/share/mame
}

plugins() {
	pkgdesc="Distribution plugins for MAME"

	amove usr/share/mame/plugins
}

tools() {
	pkgdesc="Tools for MAME"

	amove usr/bin/castool \
		usr/bin/chdman \
		usr/bin/floptool \
		usr/bin/imgtool \
		usr/bin/jedutil \
		usr/bin/ldresample \
		usr/bin/ldverify \
		usr/bin/romcmp
}

lang() {
	pkgdesc="Localization files for MAME"

	amove usr/share/mame/language
}

sha512sums="
5d613f5057cbd30e20bd47912e31db25d284b75e768228d637a740c2d5303297a0d53caff79f38b1dd3f691e1c1978f9e3c3b20158c61f71b94d879fc9c8ddf5  mame0251.tar.gz
c99a4b0dc5eed6126a9b1237b824d16a19267b61041e90637e9566abe0ca32376478c3cead7b32388fe253b351b842cb4b873d42477fa53fc09d4164a595d06a  gm0251sr002o.patch
dc008245cbea0b94f58d83e09bf5fd3fff04ac0e2f3a36b910a8b7633c5377419fc67a1fd366ef268e283f744d9a8d29928cfacf456b3edaa2d0b1a11d46a701  mame.ini
8f83ff5a916f4ff8e86c5afbdfe4475f7780bb36c20c78d6d029d0eb0dafd77b3471faa538aca384001d2049dc94c4df3429c67d743adde9fd6329c91e6d19a2  midi.conf
32fa59744e426a0921e44e1e792dbdc263cf8e505407adfe1dc392aa949db96bf8fce2140a3f5ede5a5ba756cf40d80887c502f52cec805c34c477ac44c21049  fix-musl.patch
3e4e1ccfc506d18cd4d78ea477b53bf9854568c1eab64e6dc63bbdc750dcd94a2229635d17dbe9c5ee75b28baffeac5c87850f7c87b0dd400636811a961d8d9d  nonag.patch
"
